#!/bin/bash
set -e

test() {
  INPUT=$1
  echo -e "\n *** TEST PROJECT: $INPUT *** \n"
  target/debug/goboscript build "$INPUT"
  unzip -o "$INPUT"/*.sb3 project.json -d "$INPUT"
  python -m json.tool --indent 4 "$INPUT"/project.json "$INPUT"/project.json
  node tools/sb3.js "$INPUT"/project.json
}

case $1 in
  "test")
    cargo build
    for INPUT in tests/*; do
      test "$INPUT"
    done
    ;;
  "compile")
    RUST_LOG=info cargo run -- build playground
    unzip -o playground/playground.sb3 project.json -d playground
    python -m json.tool --indent 4 playground/project.json playground/project.json
    node tools/sb3.js playground/project.json
    ;;
  "check")
    unzip -o playground/playground.sb3 project.json -d playground
    python -m json.tool --indent 4 playground/project.json playground/project.json
    node tools/sb3.js playground/project.json
    ;;
  "uncompile")
    unzip -o playground/playground.sb3 project.json -d playground
    python -m json.tool --indent 4 playground/project.json playground/project.json
    node tools/sb3.js playground/project.json
    ;;
  "patch")
    node tools/sb3.js playground/project.json
    jq -c . playground/project.json > project.json
    zip -ur playground/playground.sb3 project.json
    rm project.json
    ;;
  *)
    echo "Usage: tools/run {compile|uncompile|patch|check}"
    exit 1
    ;;
esac
